﻿using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class TicketCartDAO
    {
        private FilmTicketBookingSystemEntities _entity = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Get all ticketcart
        /// </summary>
        /// <author>VuongVT</author>
        /// <returns></returns>
        public List<TicketCartConcertEntity> GetAllTicketCart()
        {
            try
            {
                List<TicketCartConcertEntity> listTicketCart = new List<TicketCartConcertEntity>();
                var resultQuery = from ticketCart in _entity.TicketCarts
                                  where ticketCart.IsDeleted == false
                                  select ticketCart;
                if (resultQuery.Count() > 0)
                {
                    foreach (var item in resultQuery)
                    {
                        TicketCartConcertEntity ticketCart = new TicketCartConcertEntity();
                        ticketCart.CartId = item.CartId;
                        ticketCart.UserName = item.UserName;
                        ticketCart.TotalPrice = item.TotalPrice;
                        ticketCart.TotalTicket = item.TotalTicket;
                        ticketCart.IsPaid = item.IsPaid;
                        if (item.IsPaid == true)
                        {
                            ticketCart.StatusPayment = "Đã thanh toán";
                        }
                        else
                        {
                            ticketCart.StatusPayment = "Chưa thanh toán";
                        }
                        ticketCart.Note = item.Note;
                        ticketCart.CreateTime = item.CreateTime;
                        ticketCart.IsDeleted = item.IsDeleted;
                        listTicketCart.Add(ticketCart);
                    }

                    return listTicketCart;
                }

                return listTicketCart;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get all ticketcart is not paid
        /// </summary>
        /// <returns></returns>
        public List<TicketCartConcertEntity> GetAllTicketCartNotPaid()
        {
            try
            {
                List<TicketCartConcertEntity> listTicketCart = new List<TicketCartConcertEntity>();
                var resultQuery = from ticketCart in _entity.TicketCarts
                                  where ticketCart.IsDeleted == false
                                        && ticketCart.IsPaid == false
                                  select ticketCart;
                foreach (var item in resultQuery)
                {
                    TicketCartConcertEntity ticketCart = new TicketCartConcertEntity();
                    ticketCart.CartId = item.CartId;
                    ticketCart.UserName = item.UserName;
                    ticketCart.TotalPrice = item.TotalPrice;
                    ticketCart.TotalTicket = item.TotalTicket;
                    ticketCart.IsPaid = item.IsPaid;
                    if (item.IsPaid == true)
                    {
                        ticketCart.StatusPayment = "Đã thanh toán";
                    }
                    else
                    {
                        ticketCart.StatusPayment = "Chưa thanh toán";
                    }
                    ticketCart.Note = item.Note;
                    ticketCart.CreateTime = item.CreateTime;
                    ticketCart.IsDeleted = item.IsDeleted;
                    listTicketCart.Add(ticketCart);
                }

                return listTicketCart;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get TicketCart by CartId
        /// </summary>
        /// <param name="cartId">CartId</param>
        /// <returns></returns>
        public TicketCart GetTicketCartByCartId(string cartId)
        {
            try
            {
                var resultQuery = (from ticketCart in _entity.TicketCarts
                                   where ticketCart.CartId == cartId
                                         && ticketCart.IsDeleted == false
                                   select ticketCart).FirstOrDefault();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get Payment by PaymentId
        /// </summary>
        /// <param name="paymentId"></param>
        /// <returns></returns>
        public Payment GetPaymentById(int paymentId)
        {
            var resultQuery = (from payment in _entity.Payments
                               where payment.PaymentId == paymentId
                                     && payment.IsDeleted == false
                               select payment).FirstOrDefault();
            return resultQuery;
        }


        /// <summary>
        /// Get payment information by cart id
        /// </summary>
        /// <param name="cartId"></param>
        /// <returns></returns>
        public PaymentConcertEntity GetPaymentByCartId(string cartId)
        {
            PaymentConcertEntity paymentEntity = new PaymentConcertEntity();
            var resultQuery = (from payment in _entity.Payments
                               where payment.CartId == cartId
                                     && payment.IsDeleted == false
                               select payment).FirstOrDefault();
            if (resultQuery != null)
            {
                paymentEntity.PaymentId = resultQuery.PaymentId;
                paymentEntity.ChargeMoney = resultQuery.ChargeMoney;
                paymentEntity.CartId = resultQuery.CartId;
                paymentEntity.ActualMoney = resultQuery.ActualMoney;
                paymentEntity.PaidTime = resultQuery.PaidTime;
                paymentEntity.FullName = resultQuery.FullName;
                paymentEntity.BankAccount = resultQuery.BankAccount;
                paymentEntity.PhoneNumber = resultQuery.PhoneNumber;
                paymentEntity.Email = resultQuery.Email;
                paymentEntity.IsComplete = resultQuery.IsComplete;
                if (resultQuery.IsComplete == true)
                {
                    paymentEntity.StatusOfPayment = "Đã hoàn thành";
                }
                else
                {
                    paymentEntity.StatusOfPayment = "Chưa hoàn thành";
                }
                paymentEntity.ConfirmPerson = resultQuery.ConfirmPerson;
                paymentEntity.IsDeleted = resultQuery.IsDeleted;
            }
            return paymentEntity;
        }

        /// <summary>
        /// Update a payment
        /// </summary>
        /// <param name="payment"></param>
        /// <returns></returns>
        public int UpdatePayment(PaymentConcertEntity payment)
        {
            try
            {
                Payment paymentEntity = new Payment();
                paymentEntity.PaymentId = payment.PaymentId;
                paymentEntity.CartId = payment.CartId;
                paymentEntity.ChargeMoney = payment.ChargeMoney;
                paymentEntity.ActualMoney = payment.ActualMoney;
                paymentEntity.PaidTime = payment.PaidTime;
                paymentEntity.FullName = payment.FullName;
                paymentEntity.BankAccount = payment.BankAccount;
                paymentEntity.PhoneNumber = payment.PhoneNumber;
                paymentEntity.Email = payment.Email;
                paymentEntity.CartId = payment.CartId;
                if (payment.ActualMoney >= payment.ChargeMoney)
                {
                    paymentEntity.IsComplete = true;
                }
                else
                {
                    paymentEntity.IsComplete = false;
                }
                paymentEntity.ConfirmPerson = payment.ConfirmPerson;
                paymentEntity.IsDeleted = payment.IsDeleted;

                _entity.Entry(paymentEntity).State = EntityState.Modified;
                int resultQuery = _entity.SaveChanges();
                //If update payment successful -> Update property IsPaid in TicketCart table
                int resultUpdate;
                if (resultQuery == 1)
                {
                    TicketCart ticketCart = new TicketCart();
                    ticketCart = GetTicketCartByCartId(payment.CartId);
                    if (payment.ActualMoney >= payment.ChargeMoney)
                    {
                        ticketCart.IsPaid = true;
                    }
                    else
                    {
                        ticketCart.IsPaid = false;
                    }
                    _entity.Entry(ticketCart).State = EntityState.Modified;
                    resultUpdate = _entity.SaveChanges();
                }
                else
                {
                    resultUpdate = 0;
                }
                return resultUpdate;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Create a new payment information
        /// </summary>
        /// <param name="payment"></param>
        /// <returns></returns>
        public int CreatePayment(Payment payment)
        {
            try
            {
                _entity.Payments.Add(payment);
                return _entity.SaveChanges();
            }
            catch { return 0; }
        }

        /// <summary>
        /// Delete a payment
        /// </summary>
        /// <param name="payment"></param>
        /// <returns></returns>
        public int DeletePayment(Payment payment)
        {
            try
            {
                //payment.IsDeleted = true;
                //_entity.Entry(payment).State = EntityState.Modified;
                //int resultDeletePayment =  _entity.SaveChanges();
                string cartId = payment.CartId;
                _entity.Payments.Remove(payment);
                int resultDeletePayment = _entity.SaveChanges();
                int resultUpdateTicketCart = 0;
                //if (resultDeletePayment == 1)
                //{
                TicketCart ticketCart = new TicketCart();
                ticketCart = GetTicketCartByCartId(payment.CartId);
                ticketCart.IsPaid = false;
                _entity.Entry(ticketCart).State = EntityState.Modified;
                resultUpdateTicketCart = _entity.SaveChanges();
                //}
                return resultUpdateTicketCart;
            }
            catch (Exception)
            {
                return 0;
                throw;
            }
        }
    }
}